package com.ruoyi.conversation.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.user.domain.User;
import com.ruoyi.wx.utils.FileDirectoryUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.conversation.mapper.ConversationMapper;
import com.ruoyi.conversation.domain.Conversation;
import com.ruoyi.conversation.service.IConversationService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 【会话】Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-03
 */
@Service
public class ConversationServiceImpl implements IConversationService 
{
    @Autowired
    private ConversationMapper conversationMapper;

    @Value("${ruoyi.msgImage}")
    private String msgImage;

    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Conversation selectConversationById(Long id)
    {
        return conversationMapper.selectConversationById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param conversation 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Conversation> selectConversationList(Conversation conversation)
    {
        return conversationMapper.selectConversationList(conversation);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param conversation 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertConversation(Conversation conversation)
    {
        conversation.setCreateTime(DateUtils.getNowDate());
        return conversationMapper.insertConversation(conversation);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param conversation 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateConversation(Conversation conversation)
    {
        return conversationMapper.updateConversation(conversation);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteConversationByIds(Long[] ids)
    {
        return conversationMapper.deleteConversationByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteConversationById(Long id)
    {
        return conversationMapper.deleteConversationById(id);
    }

    /**
     * 根据订单id查询会话
     *
     * @param orderId
     * @return
     */
    @Override
    public List<Conversation> selectConversationByOrderId(String orderId) {
        Conversation conversation = new Conversation();
        conversation.setOrderId(orderId);
        return conversationMapper.selectConversationList(conversation);
    }

    /**
     * 上传图片聊天记录
     *
     * @param file
     * @return
     */
    @Override
    public AjaxResult uploadImageMsg(MultipartFile file) {
        if (file.isEmpty()) {
            return AjaxResult.error("请选择文件");
        }
        String fileUrl = null;
        try {
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            fileUrl = FileUploadUtils.upload(filePath, file);

        } catch (IOException e) {
            return AjaxResult.error("上传失败: " + e.getMessage());
        }
        return AjaxResult.success(fileUrl);
    }

    /**
     * 查询会话列表
     *
     * @param conversation
     * @return
     */
    @Override
    public List<Conversation> selectConversations(Conversation conversation) {
        List<Conversation> conversations = conversationMapper.selectConversations(conversation);
        for (Conversation conversation1 : conversations) {
            conversation1.setIsOffline(1);
            conversationMapper.updateConversation(conversation1);
        }
        return conversations;
    }

    @Override
    public AjaxResult refuseClose(Conversation conversation) {
        conversationMapper.deleteConversationByOrderId(conversation.getOrderId());
        return AjaxResult.success();
    }
}
